import Mock from 'mockjs'

// 生成菜单数据
const generateMenus = () => {
  return [
    {
      id: 1,
      name: '首页',
      path: '/dashboard',
      icon: 'House',
      sort: 1,
      type: 'menu',
      status: '1',
      createTime: '2024-01-01 00:00:00'
    },
    {
      id: 2,
      name: '系统管理',
      path: '/system',
      icon: 'Setting',
      sort: 2,
      type: 'menu',
      status: '1',
      createTime: '2024-01-01 00:00:00',
      children: [
        {
          id: 21,
          name: '用户管理',
          path: '/user',
          icon: 'User',
          sort: 1,
          type: 'menu',
          status: '1',
          createTime: '2024-01-01 00:00:00'
        },
        {
          id: 22,
          name: '角色管理',
          path: '/role',
          icon: 'UserFilled',
          sort: 2,
          type: 'menu',
          status: '1',
          createTime: '2024-01-01 00:00:00'
        },
        {
          id: 23,
          name: '权限管理',
          path: '/permission',
          icon: 'Lock',
          sort: 3,
          type: 'menu',
          status: '1',
          createTime: '2024-01-01 00:00:00'
        }
      ]
    },
    {
      id: 3,
      name: '内容管理',
      path: '/content',
      icon: 'Document',
      sort: 3,
      type: 'menu',
      status: '1',
      createTime: '2024-01-01 00:00:00',
      children: [
        {
          id: 31,
          name: '文章管理',
          path: '/article',
          icon: 'Document',
          sort: 1,
          type: 'menu',
          status: '1',
          createTime: '2024-01-01 00:00:00'
        },
        {
          id: 32,
          name: '分类管理',
          path: '/category',
          icon: 'Folder',
          sort: 2,
          type: 'menu',
          status: '1',
          createTime: '2024-01-01 00:00:00'
        }
      ]
    }
  ]
}

let menuList = generateMenus()

export default function() {
  // 获取用户菜单列表
  Mock.mock('/api/menu/user', 'get', () => {
    return {
      code: 200,
      message: '获取成功',
      data: menuList
    }
  })

  // 获取所有菜单列表
  Mock.mock('/api/menu/list', 'get', () => {
    return {
      code: 200,
      message: '获取成功',
      data: {
        list: menuList,
        total: menuList.length
      }
    }
  })

  // 创建菜单
  Mock.mock('/api/menu/create', 'post', (options) => {
    const menuData = JSON.parse(options.body)
    const newMenu = {
      id: Date.now(),
      ...menuData,
      createTime: Mock.Random.datetime('yyyy-MM-dd HH:mm:ss')
    }
    
    if (menuData.parentId) {
      // 添加到父级菜单的children中
      const addToParent = (list, parentId, newItem) => {
        for (let item of list) {
          if (item.id === parentId) {
            if (!item.children) item.children = []
            item.children.push(newItem)
            return true
          }
          if (item.children && addToParent(item.children, parentId, newItem)) {
            return true
          }
        }
        return false
      }
      addToParent(menuList, menuData.parentId, newMenu)
    } else {
      menuList.push(newMenu)
    }
    
    return {
      code: 200,
      message: '创建成功',
      data: newMenu
    }
  })

  // 更新菜单
  Mock.mock(/\/api\/menu\/update\/\d+/, 'put', (options) => {
    const url = options.url
    const id = parseInt(url.match(/\d+$/)[0])
    const updateData = JSON.parse(options.body)
    
    const updateInList = (list, targetId, updateData) => {
      for (let item of list) {
        if (item.id === targetId) {
          Object.assign(item, updateData)
          return true
        }
        if (item.children && updateInList(item.children, targetId, updateData)) {
          return true
        }
      }
      return false
    }
    
    if (updateInList(menuList, id, updateData)) {
      return {
        code: 200,
        message: '更新成功',
        data: null
      }
    } else {
      return {
        code: 404,
        message: '菜单不存在',
        data: null
      }
    }
  })

  // 删除菜单
  Mock.mock(/\/api\/menu\/delete\/\d+/, 'delete', (options) => {
    const url = options.url
    const id = parseInt(url.match(/\d+$/)[0])
    
    const deleteFromList = (list, targetId) => {
      for (let i = 0; i < list.length; i++) {
        if (list[i].id === targetId) {
          list.splice(i, 1)
          return true
        }
        if (list[i].children && deleteFromList(list[i].children, targetId)) {
          return true
        }
      }
      return false
    }
    
    if (deleteFromList(menuList, id)) {
      return {
        code: 200,
        message: '删除成功',
        data: null
      }
    } else {
      return {
        code: 404,
        message: '菜单不存在',
        data: null
      }
    }
  })

  // 获取菜单详情
  Mock.mock(/\/api\/menu\/detail\/\d+/, 'get', (options) => {
    const url = options.url
    const id = parseInt(url.match(/\d+$/)[0])
    
    const findInList = (list, targetId) => {
      for (let item of list) {
        if (item.id === targetId) {
          return item
        }
        if (item.children) {
          const found = findInList(item.children, targetId)
          if (found) return found
        }
      }
      return null
    }
    
    const menu = findInList(menuList, id)
    if (menu) {
      return {
        code: 200,
        message: '获取成功',
        data: menu
      }
    } else {
      return {
        code: 404,
        message: '菜单不存在',
        data: null
      }
    }
  })
} 